package com.qxsx.midi.activity.study;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.adapter.StudySearchRltListAdapter;
import com.qxsx.midi.model.StudyListItem;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class StudyClubSearchActivity extends BaseActivity {

    EditText edtSearch;
    ListView listView;
    private List<StudyListItem> list;
    private  StudySearchRltListAdapter adapter;
    String _Tag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_study_search);
        findById();
        Intent intent = getIntent();
        _Tag = intent.getStringExtra("_Tag");
        setNavBtn(R.mipmap.back, 0);
        if (_Tag.equals("Where")) {
            setTitle("搜索迷笛 CLUB");
            edtSearch.setHint("搜索迷笛Club的名字");

        } else {
            setTitle("搜索迷笛先生");
            edtSearch.setHint("搜索迷笛先生ID");

        }
        inItData();

    }

    @Override
    protected void onResume() {
        super.onResume();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (_Tag.equals("Where")) {
                    Intent intent = new Intent();
                    intent.putExtra("id", list.get(position).get_id());
                    intent.setClass(StudyClubSearchActivity.this, StudyClubDetailActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent();
                    intent.putExtra("id", list.get(position).get_id());
                    intent.setClass(StudyClubSearchActivity.this, MidiManDetailsActivity.class);
                    startActivity(intent);
                }

            }
        });
    }

    private void inItData() {
        // 列表
        list = new ArrayList<>();
        adapter = new StudySearchRltListAdapter(this, list);
        listView = (ListView) findViewById(R.id.study_search_list);
        listView.setAdapter(adapter);

        edtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    getData(v);
                    return true;
                }
                return false;
            }
        });

    }

    /**
     * 获取搜索列表
     */
    private void getData(TextView v) {
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        if (_Tag.equals("Where")) {

                        /*搜索MIDI俱乐部*/
            try {
                object.put("site_name", v.getText().toString());
                array.put(object);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            MyNet.doPostJsonArray("601", array, new callBackJsonArray() {
                @Override
                public void run(Object code, Object message, JSONArray array) {

                    if ("200".equals(code.toString())) {
                        list = new ArrayList<StudyListItem>();
                        adapter.RemoveAll();
                        try {
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject object = array.getJSONObject(i);
                                StudyListItem  sd = new StudyListItem();
                                sd.set_id(object.getString("id"));
                                sd.set_title(object.getString("exam_site_name"));
                                sd.set_desc(object.getString("address"));
                                sd.set_icon(object.getString("site_icon"));
                                list.add(sd);
                            }
                            adapter.AddData(list);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } else {
                        Log.e("code", code.toString());
                        Log.e("message", message.toString());
                    }

                }
            });
        } else {

                        /*搜索ＭＩＤＩ先生*/
            try {
                object.put("midiId", v.getText().toString());
                object.put("sex", "");
                object.put("major_id", "");
                object.put("nowpage", "1");
                object.put("pagesize","10");
                array.put(object);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            MyNet.doPostJsonArray("605", array, new callBackJsonArray() {
                @Override
                public void run(Object code, Object message, JSONArray array) {

                    if ("200".equals(code.toString())) {
                        list = new ArrayList<StudyListItem>();
                        adapter.RemoveAll();
                        list = new ArrayList<StudyListItem>();
                        try {
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject object = array.getJSONObject(i);
                                StudyListItem  sd = new StudyListItem();
                                sd.set_id(object.getString("id"));
                                sd.set_title(object.getString("major_name"));
                                sd.set_desc(object.getString("mr_introduce"));
                                sd.set_Tdesc(object.getString("real_name"));
                                sd.set_icon(object.getString("head_img"));
                                list.add(sd);
                            }
                            adapter.AddData(list);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } else {
                        Log.e("code", code.toString());
                        Log.e("message", message.toString());
                    }

                }
            });
        }

    }

    private void findById() {
        edtSearch = (EditText) findViewById(R.id.study_search);
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;
        }
    }
}
